//
//  Helper.swift
//  PeerTalkPlaygroundWithSPM
//
//  Created by Cosmetic Cygia on 2024/9/20.
//

import Foundation

class Log: Ilog {
    func v(_ tag: String, _ function: String, _ msg: String, _ line:Int) {
        printC(tag, (function + msg), line: line)
    }
    
    func v(_ tag: String, _ function: String, _ msg: String, _ tr: Error , _ line:Int) {
        printC(tag,(function + msg + " " + "\(tr)"), line: line)
    }
    
    func d(_ tag: String, _ function: String, _ msg: String , _ line:Int) {
        printC(tag, (function + msg), line: line)
    }
    
    func d(_ tag: String, _ function: String, _ msg: String, _ tr: Error , _ line:Int) {
        printC(tag, (function + msg + " " + "\(tr)"),  line: line)
    }
    
    func i(_ tag: String, _ function: String, _ msg: String , _ line:Int) {
        printC(tag, (function + msg), line: line)
    }
    
    func i(_ tag: String, _ function: String, _ msg: String, _ tr: Error? , _ line:Int) {
        printC(tag, (function + msg + " " + "\(String(describing: tr))"),  line: #line)
    }
    
    func w(_ tag: String, _ function: String, _ msg: String , _ line:Int) {
        printC(tag, (function + msg),  line: line)
    }
    
    func w(_ tag: String, _ function: String, _ msg: String, _ tr: Error , _ line:Int) {
        printC(tag, (function + msg + " " + "\(tr)"),  line: line)
    }
    
    func e(_ tag: String, _ function: String, _ msg: String , _ line:Int) {
        printC(tag, (function + msg), line: line)
    }
    
    func e(_ tag: String, _ function: String, _ msg: String, _ tr: Error , _ line:Int) {
        printC(tag, (function + msg + " " + "\(tr)"), line: line)
    }
}


//MARK: 打印显示时间，类和行数
func printC(_ tag:String,_ any:Any,line:Int) {
    //日期显示格式，可按自己需求显示
    let strNowTime = getCurrentTimeWithDateFormatString("HH:mm:ss.SSS")
    print("\(strNowTime)  \(tag)  \(any) \(line)")
}

//MARK: 获取当前时间和日期
func getCurrentTimeWithDateFormatString(_ dateFormat : String) -> String{
    let date = Date()
    let timeFormatter = DateFormatter()
    //日期显示格式，可按自己需求显示
    timeFormatter.dateFormat = dateFormat
    let strNowTime = timeFormatter.string(from: date) as String
    return strNowTime
}

func jsonDictToString(jsonDict: [String: Any]) -> String {
    do {
        let jsonData = try JSONSerialization.data(withJSONObject: jsonDict, options: [])
        
        // Convert the JSON data to a string
        if let jsonString = String(data: jsonData, encoding: .utf8) {
            
            return jsonString
        }
    } catch {
        // Handle the error
        ELog.e("Help",#function, "Error creating JSON string: \(error)", #line)
    }
    return ""
}


func toJsonDictionary(jsonString: String) -> [String : Any] {
    let js = jsonString
    //    print(js)
    guard let jsonData = js.data(using: .ascii) else {
        // Handle the case where the JSON string is invalid
        return ["auth": -1]
    }
    
    // Convert the Data to a dictionary
    do {
        if var jsonDictionary = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any] {
            // Use the jsonDictionary as needed
            jsonDictionary["auth"] = 1
            return jsonDictionary
        }
    } catch {
        // Handle the error thrown during JSON serialization
        ELog.e("Help",#function, "Error: \(error)", #line)
    }
    return ["auth": -1]
}

func getTimeStamp() -> Int64 {
    return Int64(Date().timeIntervalSince1970 * 1000)
}

func getSerialNumber() -> String {
    var serialNumber: String = "Unknown"
    let platformExpert = IOServiceGetMatchingService(kIOMainPortDefault, IOServiceMatching("IOPlatformExpertDevice"))
    
    if platformExpert > 0 {
        if let serialNumberCF = IORegistryEntryCreateCFProperty(platformExpert, kIOPlatformSerialNumberKey as CFString, kCFAllocatorDefault, 0).takeUnretainedValue() as? String {
            serialNumber = serialNumberCF.trimmingCharacters(in: .whitespacesAndNewlines)
        }
        IOObjectRelease(platformExpert)
    }
    return serialNumber
}
